﻿using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MinistryManager.Controllers;
using MinistryManager.DataAccess.Entities;
using MinistryManager.DataAccess.Services.ServiceInterfaces;
using MinistryManager.Util.Authentication;
using MinistryManager.Util.Helpers;
using Moq;

namespace MinistryManager.Tests.Login
{
    [TestClass]
    public class LoginTests
    {
        [TestMethod]
        public void TestLoginShowsLoginPage()
        {
            var controller = new LoginController();

            var view = (ViewResult)controller.Login();

            Assert.AreEqual("Login", view.ViewName, "Login View was not returned" );
        }

        [TestMethod]
        public void SendUserToLoginPageWhenUserIsInvalid()
        {
            var authForms = new Mock<IFormsAuthentication>();
            var volunteerService = GetValidUserMockUserService(null);
            var helper = new LoginHelper(authForms.Object, volunteerService.Object);

            var controller = new LoginController(helper);

            var view = (ViewResult)controller.LoginPost("user", "pass");

            Assert.AreEqual("Login", view.ViewName, "Login View was not returned");
        }

        [TestMethod]
        public void SendUserToHomeIndexPageWhenUserIsValid()
        {
            var authForms = new Mock<IFormsAuthentication>();
            var volunteerService = GetValidUserMockUserService(new Volunteer());
            var helper = new LoginHelper(authForms.Object, volunteerService.Object);

            var controller = new LoginController(helper);

            var result = (RedirectToRouteResult)controller.LoginPost("user", "pass");

            Assert.AreEqual("Home", result.RouteValues["controller"], "Index was not called");
            Assert.AreEqual("Index", result.RouteValues["action"], "Index was not called");
        }

        private Mock<IVolunteerService> GetValidUserMockUserService(Volunteer volunteer)
        {
            var volunteerService = new Mock<IVolunteerService>();
            volunteerService.Setup(us => us.GetUserByUserNameAndPassword(It.IsAny<string>(), It.IsAny<string>()))
                .Returns(volunteer);
            return volunteerService;
        }
    }
}
